package com.googlecode.tda367.denty.core.camera;

import org.jbox2d.common.Vec2;

import com.googlecode.tda367.denty.core.dynamicbody.DynamicBody;

public class MovingCamera implements Camera {

	public MovingCamera(float levelWidth, float levelHeight){
		this.levelWidth=levelWidth;
		this.levelHeight=levelHeight;
		this.xOffset=com.googlecode.tda367.denty.constants.ResolutionConstants.WIDTH;
		this.yOffset=com.googlecode.tda367.denty.constants.ResolutionConstants.HEIGHT;
		this.x=0;
		this.y=0;
	}
	
	// Constructor for testing that doesn't need the SlickFramework.
	public MovingCamera(float levelWidth, float levelHeight, float xOffset, float yOffset){
		this.levelWidth=levelWidth;
		this.levelHeight=levelHeight;
		this.xOffset = xOffset;
		this.yOffset = yOffset;
		this.x=0;
		this.y=0;
	}
	
	private float x, y, xOffset, yOffset, levelWidth, levelHeight;
	@Override
	public float getCameraX() {
		return x;
	}

	@Override
	public float getCameraXOffset() {
		return xOffset;
	}

	@Override
	public float getCameraY() {
		return y;
	}

	@Override
	public float getCameraYOffset() {
		return yOffset;
	}
	
	@Override
	public void updateCamera(Vec2 position) {
		
		x = position.x-xOffset/2;
		if (x < 0) x = 0;
		else if (x > levelWidth - xOffset) x = levelWidth - xOffset;
		y = position.y-yOffset/2;
		if (y < 0) y = 0;
		else if (y > levelHeight - yOffset) y = levelHeight - yOffset;
	}
	
	@Override
	public Vec2 getPosition(){
		return new Vec2(x, y);
	}
	
	@Override
	public void restart(){
		//doesn't need to do anything here since this camera updates dynamically
	}
	
	@Override
	public boolean isOnCamera(DynamicBody db){
		// TODO: Modify first and third checks to check for right and under edges
		Vec2 dbPos = db.getPosition();
		return (dbPos.x > x && dbPos.x < x+xOffset && dbPos.y > y && dbPos.y < y+yOffset);
	}

}
